// *****************************************************************************
//
// Copyright (C) 2013 All Right Reserved, Southwest Research Institute® (SwRI®)
//
// Contract No.  10-58058A
// Contractor    Southwest Research Institute® (SwRI®)
// Address       6220 Culebra Road, San Antonio, Texas 78228-0510
// Contact       Steve Dellenback <sdellenback@swri.org> (210) 522-3914
//
// This code was developed as part of an internal research project fully funded
// by Southwest Research Institute®.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// *****************************************************************************

#ifndef MAPVIZ_PLUGINS__GPS_PLUGIN_H_
#define MAPVIZ_PLUGINS__GPS_PLUGIN_H_

// Include mapviz_plugin.h first to ensure GL deps are included in the right order
#include <mapviz/mapviz_plugin.h>

#include <mapviz/map_canvas.h>
#include <mapviz_plugins/point_drawing_plugin.h>

// QT libraries
#include <QGLWidget>
#include <QObject>
#include <QWidget>

// ROS libraries
#include <rclcpp/rclcpp.hpp>
#include <tf2/transform_datatypes.h>
#include <gps_msgs/msg/gps_fix.hpp>
#include <swri_transform_util/local_xy_util.h>

// C++ standard libraries
#include <list>
#include <string>
#include <vector>

// QT autogenerated files
#include "ui_gps_config.h"

namespace mapviz_plugins
{
class GpsPlugin : public mapviz_plugins::PointDrawingPlugin
{
  Q_OBJECT

  public:
    GpsPlugin();
    ~GpsPlugin() override = default;

    bool Initialize(QGLWidget* canvas) override;
    void Shutdown() override {}

    void Draw(double x, double y, double scale) override;

    void LoadConfig(const YAML::Node& node, const std::string& path) override;
    void SaveConfig(YAML::Emitter& emitter, const std::string& path) override;

    QWidget* GetConfigWidget(QWidget* parent) override;

  protected:
    void PrintError(const std::string& message) override;
    void PrintInfo(const std::string& message) override;
    void PrintWarning(const std::string& message) override;

  protected Q_SLOTS:
    void SelectTopic();
    void TopicEdited();

  private:
    Ui::gps_config ui_;
    QWidget* config_widget_;

    std::string topic_;
    rmw_qos_profile_t qos_;

    // ros::Subscriber gps_sub_;
    rclcpp::Subscription<gps_msgs::msg::GPSFix>::SharedPtr gps_sub_;
    bool has_message_;

    void GPSFixCallback(const gps_msgs::msg::GPSFix::SharedPtr gps);
    void connectCallback(const std::string& topic, const rmw_qos_profile_t& qos);
};
}   // namespace mapviz_plugins

#endif  // MAPVIZ_PLUGINS__GPS_PLUGIN_H_
